#![allow(non_snake_case)]
use leptos::server;
use server_fn::ServerFnError;
use shq_common::prelude::Zjfp;
use shq_web_controls::SelectOption;

//获取条线
#[server(ZxjlZbInsert, "/api")]
pub async fn zxjl_conf_zb() -> Result<Vec<SelectOption<String>>, ServerFnError> {
    use shq_pas_backend::zxjl_conf_zb;
    let tree_nodes = zxjl_conf_zb()
        .await
        .map_err(|e| ServerFnError::new(format!("获取条线失败：{}", e)))?;
    let options = tree_nodes
        .into_iter()
        .map(|node| SelectOption::new(node.title, node.key))
        .collect();

    Ok(options)
}

//保存分配
#[server(ZxjlFp, "/api")]
pub async fn zxjl_fp_insert(
    ny: i32,
    cgid: i32,
    yfpe: f64,
    emid: i32,
    ffny: i32,
    zjmc: String,
    txid: i32,
) -> Result<(), ServerFnError> {
    use shq_pas_backend::zxjl_fp_insert;
    zxjl_fp_insert(ny, cgid, yfpe, emid, ffny, zjmc, txid)
        .await
        .map_err(|e| ServerFnError::new(format!("分配失败：{}", e)))?;
    Ok(())
}

//获取分配结果
#[server(ZxjlFpJg, "/api")]
pub async fn zxjl_fpjg(cgid: i32) ->  Result<Vec<Zjfp>, ServerFnError> {
    use shq_pas_backend::zxjl_fpjg;
     let fpjg = zxjl_fpjg(cgid)
        .await
        .map_err(|e| ServerFnError::new(format!("获取失败：{}", e)))?;
    Ok(fpjg)
}

//是否终止
#[server(ZxjlZz, "/api")]
pub async fn zxjl_zz(ny: i32) -> Result<(), ServerFnError> {
    use shq_pas_backend::zxjl_zz;
    zxjl_zz(ny)
        .await
        .map_err(|e| ServerFnError::new(format!("终止失败：{}", e)))?;
    Ok(())
}
